package com.practice.J2E.thread;

import cn.hutool.core.thread.ThreadUtil;

/**
 * @author kayn
 * @create 2023-09-21 上午 09:02
 */
public class Dog implements Runnable{

    public final Bread left;
    public final Bread right;

    public String name;

    public Dog(Bread left, Bread right, String name) {
        this.left = left;
        this.right = right;
        this.name = name;
    }

    @Override
    public void run() {
        synchronized (left) {

            Thread thread = Thread.currentThread();

            System.out.println(name + "左手拿到了" + left.getName() + "。右手准备拿" + right.getName());

            ThreadUtil.sleep(1000);

            synchronized (right) {

                ThreadUtil.sleep(1000);

                System.out.println(name + "吃到面包了");

            }

        }
    }
}
